/**
 * 角色权限编辑
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author qinyifeng
 * @since 2019/03/18
 */

var treeGrid = null, tableId = 'zmn-role-permit-table';
layui.extend({
    treeGrid: '{/}/static/js/layui/treeGrid/treeGrid',
}).define(['index', 'jquery', 'treeGrid', 'layer'], function (exports) {
    treeGrid = layui.treeGrid;//很重要
    var data = {roleId: json.roleId, sysId: json.sysId};
    treeGrid.render({
        id: tableId
        , elem: '#' + tableId
        , url: '/mc/role/permit/tree.action'
        , where: data
        , cellMinWidth: 100
        , idField: 'permitId'//必須字段
        , treeId: 'permitId'//树形id字段名称
        , treeUpId: 'parentId'//树形父id字段名称
        , treeShowName: 'permitName'//以树形式显示的字段
        , heightRemove: [".dHead", 10]//不计算的高度,表格设定的是固定高度，此项不生效
        , height: '100%'
        , isFilter: false
        , iconOpen: true//是否显示图标【默认显示】
        , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
        , loading: true
        , method: 'post'
        , isPage: false
        , toolbar: "#zmn-role-permit-table-tool"
        , cols: [[
            //{type: 'numbers'},
            {type: 'checkbox', sort: true}
            ,{field: 'permitName', width: '41%', title: '权限名称', sort: true}
            ,{field: 'remark',width:'55%', title: '备注'}
        ]]
        , parseData: function (res) {//数据加载后回调
            return res;
        }
    });

    //对外暴露的接口
    exports('art_role_permit_list', {});
});

/**
 * 保存角色权限
 */
function save() {
    var checkStatus = treeGrid.checkStatus(tableId)
        , data = checkStatus.data;
    var permitIds = "";
    layui.each(data, function (i, item) {
        if (i > 0) {
            permitIds += ",";
        }
        permitIds += item.permitId;
    });
    zmnPage.ajaxSimple("/mc/role/permit/save.action", {ids: permitIds, roleId: json.roleId, sysId: json.sysId}, 1500);
}

/**
 * 展开或折叠全部菜单
 */
function openAll() {
    var treedata = treeGrid.getDataTreeList(tableId);
    treeGrid.treeOpenAll(tableId, !treedata[0][treeGrid.config.cols.isOpen]);
}
